package com.wf.ew.system.biz.impl;

import com.wf.ew.common.BaseDao;
import com.wf.ew.common.PageResult;
import com.wf.ew.common.utils.Utils;
import com.wf.ew.system.biz.DictionarydataBiz;
import com.wf.ew.system.model.Dictionarydata;
import com.wf.ew.system.model.User;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2019-08-06 16:09:47
 * @version ：1.0
 */
@Repository
public class DictionarydataBizimpl extends BaseDao implements DictionarydataBiz {

	@Override
	public String list(PageResult<T> page, Dictionarydata dictionarydata) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Dictionarydata> rm = new BeanPropertyRowMapper<Dictionarydata>(Dictionarydata.class);
		
		StringBuffer sql = new StringBuffer("select d.id, d.dictdata_code, d.dict_code, dc.dict_name, "
			+ "d.dictdata_name, d.sort_number,"
			+" d.description, d.createTime, d.createUser,u.name createName, d.updateTime, d.updateUser "
			+ " from sys_dictionary_data d "
			+ " inner join sys_dictionary dc on dc.dict_code=d.dict_code "
			+ " left join sys_user u on u.id=d.createUser "
			+ " where d.del=0 ");
		if(Utils.isNotEmpty(dictionarydata)){
			if(Utils.isNotEmpty(dictionarydata.getDict_code())){
				sql.append(" and d.dict_code=:dict_code ");
				map.put("dict_code", dictionarydata.getDict_code());
			}
		}
		sql.append(" order by sort_number asc");
		return jsonPage(getPageQueryList(page, sql.toString(), map, rm));
	}
	@Override
    public List<Dictionarydata> list(Dictionarydata dictionarydata) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Dictionarydata> rm = new BeanPropertyRowMapper<Dictionarydata>(Dictionarydata.class);

		StringBuffer sql = new StringBuffer("select d.id, d.dictdata_code, d.dict_code, dc.dict_name," +
				" d.dictdata_name, d.sort_number,"
			+" d.description, d.createTime, d.createUser,u.name createName, d.updateTime, d.updateUser "
			+ " from sys_dictionary_data d "
			+ " inner join sys_dictionary dc on dc.dict_code=d.dict_code "
			+ " left join sys_user u on u.id=d.createUser "
			+ " where d.del=0 and dc.state='normal' ");
		if(Utils.isNotEmpty(dictionarydata)){
			if(Utils.isNotEmpty(dictionarydata.getDict_code())){
				sql.append(" and dc.dict_code=:dict_code ");
				map.put("dict_code", dictionarydata.getDict_code());
			}
		}
		sql.append(" order by sort_number asc");
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public Dictionarydata getById(Dictionarydata dictionarydata) {
		RowMapper<Dictionarydata> rm = new BeanPropertyRowMapper<Dictionarydata>(Dictionarydata.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select d.id, d.dictdata_code, d.dict_code, " +
				"d.dictdata_name, d.sort_number,"
			+" d.description, d.createTime, d.createUser, d.updateTime, d.updateUser"
			+" "
			+ "from sys_dictionary_data d where d.del=0 ");
		if(Utils.isNotEmpty(dictionarydata)){
			if(Utils.isNotEmpty(dictionarydata.getId())){
				sql.append("and d.id=:id ");
				map.put("id", dictionarydata.getId());
			}
			if(Utils.isNotEmpty(dictionarydata.getDictdata_code())){
				sql.append("and d.dictdata_code=:dictdata_code ");
				map.put("dictdata_code", dictionarydata.getDictdata_code());
			}
		}

		getMapParameter(sql, map);
        try{
            return jdbcTemplate.queryForObject(sql.toString(), map, rm);
        }catch (Exception e){
            return null;
        }
	}

	@Override
	public boolean add(Dictionarydata dictionarydata, User loginUser) {
		int count = 0;
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into sys_dictionary_data (dictdata_code,dict_code,dictdata_name,sort_number "
			+",description,createUser )");
		sql.append("values (:dictdata_code,:dict_code,:dictdata_name,:sort_number "
			+",:description,:createUser )");
		map.put("dictdata_code", dictionarydata.getDictdata_code());
		map.put("dict_code", dictionarydata.getDict_code());
		map.put("dictdata_name", dictionarydata.getDictdata_name());
		map.put("sort_number", dictionarydata.getSort_number()==null?"1":dictionarydata.getSort_number()+"");
		map.put("description", dictionarydata.getDescription());
		map.put("createUser", loginUser.getId());
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(Dictionarydata dictionarydata, User loginUser) {
		int count = 0;
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update sys_dictionary_data set dictdata_code=:dictdata_code ");
		sql.append(", dictdata_name=:dictdata_name, sort_number=:sort_number ");
		sql.append(", description=:description");
		sql.append(", updateUser=:updateUser, updateTime=now() where id=:id");
		map.put("dictdata_code", dictionarydata.getDictdata_code());
		map.put("dictdata_name", dictionarydata.getDictdata_name());
		map.put("sort_number", dictionarydata.getSort_number()==null?null:dictionarydata.getSort_number()+"");
		map.put("description", dictionarydata.getDescription());
		map.put("updateUser", loginUser.getId());
		map.put("id", dictionarydata.getId());
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean delete(Dictionarydata dictionarydata, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(dictionarydata.getIds().length()>0){
			for(String ID : dictionarydata.getIds().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update sys_dictionary_data set del=1" +
						",updateUser=:updateUser, updateTime=now() " 
						+ " where id =:id ");
				map.put("updateUser", loginUser.getId());
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
